package com.linkoog.kit;

public class LeftTrimKit {


    /**
     *    删除每行前指定数量的空格
     */
    public static String removeLeadingSpaces(String source, int spacesNumToRemove) {
        if (source == null || source.isEmpty()) return source;
        if (spacesNumToRemove <= 0) return source;

        String[] lines = source.split("\n");
        StringBuilder resultBuilder = new StringBuilder();
        for (String line : lines) {
            int validLength = Math.min(spacesNumToRemove, line.length());
            int trimLength = 0;
            for (int i = 0; i < validLength; i++) {
                char c = line.charAt(i);
                if (c == ' '){
                    trimLength = i;
                } else {
                    break;
                }
            }

            String trimmedLine = line.substring(trimLength + 1);
            resultBuilder.append(trimmedLine).append("\n");
        }

        if (! source.endsWith("\n")) {
            // 移除最后一个换行符
            resultBuilder.deleteCharAt(resultBuilder.length() - 1);
        }

        // 返回处理后的结果
        return resultBuilder.toString();
    }

}
